<?php
global $Config;
require_once($Config->WebCMSRoot.'Base/Control.php');
require_once($Config->WebCMSRoot.'Views/Blog.php');

/*
* Blogger API:       http://code.google.com/apis/blogger/overview.html
* Developer's Guide: http://code.google.com/apis/blogger/developers_guide_protocol.html
* Reference Guide:   http://code.google.com/apis/blogger/reference.html
*/
/* TODO:
 - move parameters to page or proxy-control
*/

Class Blog extends BaseControl
{
	var $view;
	var $blogID;
	var $blogURL;
	var $pageCount;
	//var $blogUser;
	//var $blogPassword;

	function Blog($parameters, &$page)
	{
//print('<pre>'.var_export($parameters, true).'</pre>');
//print('<pre>'.var_export($this, true).'</pre>');exit;
		$this->blogID = "499741689162198889";
		$this->blogURL = "http://www.blogger.com";
		$this->blogSpotURL = "http://familieboden.blogspot.com";
		$this->pageCount = 5;
		//$this->blogUser = "ronald.anthonissen@gmail.com";
		//$this->blogPassword = "racv23296";

		$this->view = new BlogView();

		parent::BaseControl($parameters, &$page);
//print('<pre>'.var_export($this, true).'</pre>');exit;
	}

	function Execute()
	{
//print('<pre>'.var_export($this, true).'</pre>');exit;

		switch ($this->GetParameter('show'))
		{
			case 'index':
				$posts = $this->GetSummary($this->pageCount);
				$this->view->PrintSummaries(&$posts, $this->error);
				//$posts = $this->GetFull();
				//$this->view->PrintIndex(&$posts);  // Messes up HTML
				break;
			case 'post':
//print('<pre>'.var_export($this->parameters, true).'</pre>');exit;
				$posts = $this->GetPost($this->GetParameter('id'));
				$comments = $this->GetComments($this->GetParameter('id'));
//print('<pre>'.var_export($comments, true).'</pre>');exit;
				$this->view->PrintPost(&$posts, $comments, $this->blogID, $this->GetParameter('id'), $this->error);
				break;
				
			case 'page':
				$posts = $this->GetSummary($this->pageCount, $this->GetParameter('start'));
				$this->view->PrintSummaries(&$posts, $this->error);
				break;
		}

		parent::Execute();
	}

	function ValidateArguments($args)
	{
//print('<pre>'.var_export($this, true).'</pre>');exit;
		$valid = false;
		
		switch ($this->GetParameter('show'))
		{
			case 'index':
				$valid = (count($args) == 1);
				break;
			case 'post':
				$valid = (count($args) == 2);
				break;
			case 'page':
				$valid = (count($args) == 2);
				break;
		}

		return $valid;
	}
	
	function GetSummary($count, $start=1)
	{
		$url = $this->blogURL . '/feeds/' . $this->blogID . '/posts/summary/?max-results=' . $count . '&start-index=' . $start;

		return $this->GetBloggerRequest($url);
	}
	
	function GetFull()
	{
		$url = $this->blogURL . '/feeds/' . $this->blogID . '/posts/full';
		
		return $this->GetBloggerRequest($url);
	}
	
	function GetPost($postID)
	{
		$url = $this->blogURL . '/feeds/' . $this->blogID . '/posts/full/' . $postID;
		
		return $this->GetBloggerRequest($url);
	}

	function GetComments($postID)
	{
		$url = $this->blogSpotURL . '/feeds/' . $postID . '/comments/full';
		
		return $this->GetBloggerRequest($url);
	}

// TODO: can these be requested with the query?	
//<WebCMS:Blog show="post" title="$$1$$" />
//<WebCMS:Blog show="archive" year="$$1$$" />
//<WebCMS:Blog show="archive" year="$$1$$" month="$$2$$" />

	function GetBloggerRequest($url)
	{
//print('<a href="'.$url.'">'.$url.'</a><br />');
		$ch = curl_init();

		// set URL and other appropriate options
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_HEADER, 0);
/*if (isset($_ENV['COMPUTERNAME']))
{
	switch ($_ENV['COMPUTERNAME'])
	{
		case 'BE-A2W-082':
			curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
			curl_setopt($ch, CURLOPT_PROXY, 's-alcis01:8080');
			curl_setopt($ch, CURLOPT_PROXYUSERPWD, '2WHEELS\ronald.anthonissen:Ron@ld08');
			break;
		case 'WXP-ANTHORO':
			curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 0);
			curl_setopt($ch, CURLOPT_PROXY, '10.0.10.2:8080');
			//curl_setopt($ch, CURLOPT_PROXYUSERPWD, '2WHEELS\ronald.anthonissen:Ron@ld08');
			break;
	}
}*/
		//curl_setopt($ch, CURLOPT_USERPWD, $this->blogUser.':'.$this->blogPassword);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

		// grab URL and pass it to the browser
		$response = curl_exec($ch);
		//$response = utf8_decode($response);
//print('<pre>'.curl_errno($ch).': '.curl_error($ch).'</pre>');exit;
		$this->error = array(curl_errno($ch), curl_error($ch));
//print('<pre>'.var_export($error, true).'</pre>');exit;

		// close curl resource, and free up system resources
		curl_close($ch);
		
//print('<pre>'.var_export($response, true).'</pre>');exit;
		return $response;
	}
}
?>